﻿//----------------------------------------------------------------------------------------------------
// The PlayerManager is responsible for handling players
//----------------------------------------------------------------------------------------------------

package HG.Managers
{
	import HG.GameService;
	import HG.Managers.LevelManager;
	
	import HG.GameDataTypes.Player;
	
	import HG.Debug;
	
	import HG.Events.PlayerEvent;
	import HG.Events.NodeEvent;
	
	public class PlayerManager extends IGameManager
	{
		// Player types
		public static const PLAYER_ENVIRONMENT = 0;
		public static const PLAYER_HUMAN = 1;
		public static const PLAYER_COMPUTER = 2;
		
		var m_players:Array;
		var m_playerIDs:Array;
		var m_currentPlayer:Number;
		var m_playerNumberMap:Array;
		
		var m_pEvt:PlayerEvent;
		
		//----------------------------------------------------------------------------------------------------
		public function PlayerManager()
		{
			m_players = new Array();
			m_playerIDs = new Array();
			m_playerNumberMap = new Array();
			m_currentPlayer = -1;
		}
			
		//----------------------------------------------------------------------------------------------------
		override public function Initialize()
		{
		}
		
		//----------------------------------------------------------------------------------------------------
		override public function Shutdown()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		override public function OnTick(DeltaTime:Number)
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		//Events
		//----------------------------------------------------------------------------------------------------
		private function RaisePlayerJoinEvent(ID:Number)
		{
			m_pEvt = new PlayerEvent(PlayerEvent.JOIN);
			m_pEvt.m_playerNumber = ID;
			dispatchEvent(m_pEvt);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function RaisePlayerFateEvent(ID:Number, fate:Number)
		{
			m_pEvt = new PlayerEvent(PlayerEvent.FATE);
			m_pEvt.m_playerNumber = ID;
			m_pEvt.m_fate = fate;
			dispatchEvent(m_pEvt);
		}
		
		//----------------------------------------------------------------------------------------------------
		// Internal
		//----------------------------------------------------------------------------------------------------
		public function AddPlayer(type:Number, rootNode:Number):Number
		{
			var newPlayerID:Number = m_players.length;
			
			m_players[newPlayerID] = new Player(newPlayerID);
			m_players[newPlayerID].SetType(type);
			m_players[newPlayerID].SetRootNode(rootNode);
			
			m_playerIDs.push(newPlayerID);
			
			if (type == PLAYER_HUMAN)
				m_currentPlayer = newPlayerID;
			
			RaisePlayerJoinEvent(newPlayerID);
			
			return newPlayerID;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetPlayerFromRootNode(nodeID:Number):Number
		{
			var index:Number = 0;
			var listLength:Number = m_playerIDs.length;
			
			for (index = 0; index < listLength; ++index)
			{
				if (m_players[m_playerIDs[index]].GetRootNode() == nodeID)
				{
					return m_playerIDs[index];
				}
			}
			
			return -1;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function FlushPlayers()
		{
			m_currentPlayer = -1;
			m_players = new Array();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetPlayers():Array
		{
			return m_players;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetPlayerIDs():Array
		{
			return m_playerIDs;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetPlayerExists(ID:Number):Boolean
		{
			var player:Player = m_players[ID];
			
			if (!player)
				return false;
			
			return true;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetPlayer(ID:Number):Player
		{
			var player:Player = m_players[ID];
			
			Debug.ASSERT(player, "Invalid player fetch! " + ID);
			
			return player;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetCurrentPlayerID(ID:Number)
		{
			m_currentPlayer = ID;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetCurrentPlayerID():Number
		{
			return m_currentPlayer;
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}